/**
 * @author  steven.wang
 * @date	2018/12/25
 * @description
 */
const fs = require("fs");
const cheerio = require('cheerio');
const asyncQuene = require("async").queue;
const request = require('superagent');
require('superagent-charset')(request);
const tools = require('./tools');
const config = require('./config');

/**
 * 获取所有画册URL
 * @author steven.wang
 * @DateTime 2019-02-27T11:07:41+0800
 */
const getAlbumsAsync = () => {
    return new Promise((resolve, reject) => {
        console.log('Start get albums .....');
        let albums = [];
        let q = asyncQuene(async (pageUrl, cb) => {
            try {
                let $ = await tools.getHtmlAsync(pageUrl.url, 'gb2312');
                console.log(`download ${pageUrl.url} success`);
                $('.pic-list>ul>li').each(function (idx, elem) {
                    albums.push({
                        type: pageUrl.type,
                        title: $(this).find('.name a').attr('title') ||
                            $(this).find('img').attr('alt').replace(/\//g, '').replace(/\.html/g, ''),
                        url: config.lr08.host + $(this).find('.name a').attr('href'),
                        imgList: []
                    });
                });
            } catch (err) {
                console.log(`Error : get Album list - download ${pageUrl.url} err : ${err}`);
            } finally {
                cb();
            }
        }, 10);
        /**
         * 监听：当所有任务都执行完以后，将调用该函数
         */
        q.drain = () => {
            console.log('Get album list complete');
            resolve(albums);
        }

        let pageUrls = [];
        for (let item of config.lr08.list) {
            for (let i = item.startPage; i <= item.endPage; i++) {
                pageUrls.push({
                    type: item.type,
                    url: item.url + `${i}.html`
                });
            }
        }
        q.push(pageUrls);
    });
}
/**
 * 根据画册URL获取画册里的所有图片URL
 * @author steven.wang
 * @DateTime 2019-02-27T11:08:05+0800
 */
const getImageListAsync = albumsList => {
    return new Promise(function (resolve, reject) {
        console.log('Start get album`s imgList ....');
        let q = asyncQuene(async function ({ url: albumUrl, title: albumTitle, type: albumType, imgList }, cb) {
            try {
                let $ = await tools.getHtmlAsync(albumUrl, 'gb2312');
                console.log(`get album ${albumTitle} image list done`);
                $('#pictureurls li img').each(function (idx, element) {
                    imgList.push('http://www.08lr.cn' + $(this).attr('rel'));
                });
            } catch (err) {
                console.log(`Error :get image list - download ${albumUrl} err : ${err}`);
            }
            finally {
                cb();
            }
        }, 10);
        q.drain = function () {
            console.log('Get image list complete');
            resolve(albumsList);
        }
        q.push(albumsList);
    });
}

async function app() {
    // 下载08绿软站
    let albumList = await getAlbumsAsync();
    albumList = await getImageListAsync(albumList);
    let values = [];
    for (let item of albumList) {
        let count = 0;
        for (let subItem of item.imgList) {
            values.push([
                item.type,
                config.lr08.from,
                item.title + ++count,
                subItem,
                subItem
            ]);
        }
    }
    tools.writeJsonToFile(config.lr08.path, albumList);
    // 存入数据库
    let insertResult = await tools.insertPhoto(values);
    console.log(insertResult)
    // if (config.downloadImg) {
    //     downloadImg(albumList);
    // }
}

app();
